package com.rem.chat.controller;

import com.rem.chat.aop.annotation.GlobalInterceptor;
import com.rem.chat.common.utils.JacksonUtil;
import com.rem.chat.pojo.request.PageRequest;
import com.rem.chat.pojo.request.UpdateAddRequest;
import com.rem.chat.pojo.request.UpdateModifyRequest;
import com.rem.chat.pojo.request.UpdatePostRequest;
import com.rem.chat.pojo.response.CheckVersionResponse;
import com.rem.chat.pojo.response.CommonResult;
import com.rem.chat.pojo.response.LoadAllUpdateResponse;
import com.rem.chat.service.UpdateService;
import jakarta.annotation.Nullable;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotNull;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;

/**
 * @author: Themberfue
 * @date: 2025/6/11 12:56
 * @description:
 */
@RestController
@RequestMapping("/admin/update")
public class UpdateController {
    private static final Logger log = LoggerFactory.getLogger(UpdateController.class);

    @Resource(name = "updateServiceImpl")
    UpdateService updateService;

    @GetMapping("/loadAllUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public CommonResult<LoadAllUpdateResponse> loadAllUpdate(@RequestBody @Nullable @Validated PageRequest param) {
        log.info("loadAllGroup PageRequest:{}", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(
                updateService.loadAllUpdate(param));
    }

    @PostMapping("/addUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public CommonResult<Boolean> addUpdate(@RequestPart @Validated UpdateAddRequest param,
                                           @Nullable MultipartFile file,
                                           HttpServletRequest request) throws IOException {
        log.info("addUpdate UpdateAddRequest:{}", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(
                updateService.addUpdate(param, file));
    }

    @PostMapping("/modifyUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public CommonResult<Boolean> modifyUpdate(@RequestPart @Validated UpdateModifyRequest param,
                                              @Nullable MultipartFile file,
                                              HttpServletRequest request) throws IOException {
        log.info("modifyUpdate UpdateModifyRequest:{}", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(
                updateService.modifyUpdate(param, file));
    }

    @DeleteMapping("/delUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public CommonResult<Boolean> delUpdate(@NotNull Integer id) throws IOException {
        log.info("delUpdate id:{}", id);

        return CommonResult.success(
                updateService.delUpdate(id));
    }

    @PutMapping("/postUpdate")
    @GlobalInterceptor(checkAdmin = true)
    public CommonResult<Boolean> postUpdate(@RequestBody @Validated UpdatePostRequest param) {
        log.info("postUpdate UpdatePostRequest:{}", JacksonUtil.writeValueAsString(param));

        return CommonResult.success(
                updateService.postUpdate(param));
    }

    @PostMapping("/checkVersion")
    @GlobalInterceptor
    public CommonResult<CheckVersionResponse> checkVersion(@NotBlank(message = "版本号不能为空") String version,
                                                           @NotBlank(message = "用户id不能为空") String userId) {
        log.info("checkVersion version:{}, userId:{}", version, userId);

        return CommonResult.success(updateService.checkVersion(version, userId));
    }
}
